const fetch = require("node-fetch");

const wordlist = async (ctx, next) => {
  const { redis, query } = ctx
  const { sync } = query;
  const result = await redis.get("mywords")
  if (typeof result === 'string' && sync !== '1') {
    ctx.response.body = JSON.parse(result);
    return
  }

  const url = `https://dict.youdao.com/wordbook/webapi/v2/word/list?limit=480&offset=0&sort=time&lanTo=&lanFrom=`;
  const res = await fetch(url, {
    method: "GET",
    headers: {
      Cookie: `OUTFOX_SEARCH_USER_ID_NCOO=47606896.167317815; __yadk_uid=LL72nTr0LoGuSxL31WqtaR7gXStTGSmn; OUTFOX_SEARCH_USER_ID=1588019451@153.34.179.117; rollNum=true; NTES_SESS=EvtQm9MLPg66MUGiHL9AQjv7PIlso4KYDlujl6kLJU6UXu9MX1ahwnrudj_co_noBFlXQSPEzj0wuZIKu6Ra.ZXBsNLfpvBtKMzWLDs3VUOYCAz.2KgENGIe83JPZ3mOqqUhqnbF9HnM2QShpeZrjzkLMss.dN5U1nKGQebFP2Zyv7h6UD7V8Ud.usI2yztCNihLLCSX1Uy8Cldu3uiKGMHz1YdOycv6Z; S_INFO=1696050225|0|3&80##|jack_crysler; P_INFO=jack_crysler@163.com|1696050225|0|dict_logon|00&99|jis&1690539898&mail163#jis&320100#10#0#0|132179&0||jack_crysler@163.com; DICT_SESS=v2|cSEVYQlSJBeKOfYWhMll0zYh4YG6MOY0J4PLq4nfzGRTK0MgF6MpZ0OE0MJS6LOE0OA6MqykMkE0llOMUGOMPBRgLn4gBkfq4R; DICT_PERS=v2|urscookie||DICT||web||-1||1696050225919||122.97.154.226||jack_crysler@163.com||p4nMOEPMYf0pZ6MgzOfPB0JzhMY5P4Ju0qZ0MYlO4kG0wLRLQLkMJK0euOMJFhMQK0lA6LwunLOERY5RfOm0LwZ0; DICT_LOGIN=7||1696050225929; DICT_UT=urs-phoneyd.8ab7b7bd56b541deb@163.com; ___rl__test__cookies=1696050226143`,
    },
  }).then((res) => res.json());
  redis.set("mywords", JSON.stringify(res));

  ctx.response.body = res;
};

wordlist.method = 'get'
wordlist.name = 'wordlist'

module.exports = wordlist;
